//
//  BookBomView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/13.
//

import UIKit

class BookBomView: UIView {
    
    lazy var readBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("START READING", for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.numberOfLines = 2
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("read")
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: ScreenW-81, height: 41))
        btn.wn_cornerWithBorder(20.5)
        return btn
    }()
    
    lazy var colBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "book_collect"), for: .normal)
        btn.setImage(UIImage(named: "book_collect_sel"), for: .selected)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("collect")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var btnClosure: StringClosure?
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                if m.durChapterId == 0 {
                    readBtn.setTitle("START READING", for: .normal)
                }else {
                    readBtn.setTitle("CONTINUE READING\n\(m.durChapterName ?? "")", for: .normal)
                }
                colBtn.isSelected = model?.favorite ?? false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(colBtn)
        addSubview(readBtn)
        colBtn.snp.makeConstraints { (make) in
            make.top.equalTo(4)
            make.right.equalTo(-12)
            make.width.height.equalTo(41)
        }
        readBtn.snp.makeConstraints { (make) in
            make.top.equalTo(6)
            make.left.equalTo(12)
            make.right.equalTo(colBtn.snp.left).offset(-16)
            make.height.equalTo(41)
        }
        wn_cornerWithBorder(0, 0, .white, 1, .init(white: 0, alpha: 0.1), .init(width: 0, height: 1), 5)
        layer.masksToBounds = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
